package opengles;

import android.content.Context;
import android.opengl.GLES20;

import com.scu.freehyan.glesframebuffer.R;

import GLESUtils.Utils;

/**
 * Created by freehyan on 9/8/2016.
 */
public class PrimaryPass extends GLESPass{
    private int texture;

    public PrimaryPass(int texture, Context activityContext) {
        super(activityContext, Coordinates.OpenGLTextureVertices);
        this.texture = texture;

        String VertexShader = Utils.getInstance().readTextFileFromRawResource(activityContext, R.raw.vertshowtexture);
        String FragShader = Utils.getInstance().readTextFileFromRawResource(activityContext, R.raw.fragshowtexture);
        int VShader = mPassProgram.loadShader(GLES20.GL_VERTEX_SHADER, VertexShader);
        int FShader = mPassProgram.loadShader(GLES20.GL_FRAGMENT_SHADER, FragShader);
        mPassProgram.compileShader(VShader, FShader);
    }

    @Override
    public void render() {
        int ProgramId = mPassProgram.getProgram();
        GLES20.glUseProgram(ProgramId);
//        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
//        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);

        mPositionHandle = GLES20.glGetAttribLocation(ProgramId, "a_Position");
        mTextureCoordHandle = GLES20.glGetAttribLocation(ProgramId, "a_TexCoord");


        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

        GLES20.glEnableVertexAttribArray(mTextureCoordHandle);
        GLES20.glVertexAttribPointer(mTextureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, textureVerticesBuffer);

       // GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        mTexSamplerHandle = GLES20.glGetUniformLocation(ProgramId, "s_texture");
        GLES20.glUniform1i(mTexSamplerHandle, 0);
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mTextureCoordHandle);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glUseProgram(0);
    }
}
